package com.yitongmao.core.network.api;


import com.yitongmao.AppConstancts;
import com.yitongmao.core.glide.ProgressListener;
import com.yitongmao.core.glide.ProgressResponseBody;
import com.yitongmao.core.network.api.gosn.GsonConverterFactory;
import com.yitongmao.core.network.httpclient.OkHttpClientFactory;
import com.yitongmao.util.Loger;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;


public final class ApiGenerator {
    private ApiGenerator() {

    }

    public static OkHttpClient httpClient;
    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(AppConstancts.Net.baseicon)//这里需要些baseUrl地址，请求地址
                    .addConverterFactory(new ToMediaConverterFactory())
                    .addConverterFactory(GsonConverterFactory.create());
    public static <T> T createApiClient(Class<T> serviceClass) {
        return createApiClient(serviceClass, null);
    }
    public static <T> T createApiClients(Class<T> serviceClass,ProgressListener progressListener) {
        return createApiClient(serviceClass, null,null,progressListener);
    }
    public static <T> T createApiClient(Class<T> serviceClass, String baseUrl) {
        return createApiClient(serviceClass, baseUrl, null,null);
    }

    public static <T> T createApiClient(Class<T> serviceClass, String baseUrl, HashMap headers,final ProgressListener progressListener) {
        if (!isEmptry(baseUrl)) {
            builder =
                    new Retrofit.Builder()
                            .baseUrl(baseUrl)
                            .addConverterFactory(new ToMediaConverterFactory())
                            .addConverterFactory(GsonConverterFactory.create());
        }
        if(httpClient == null){
            if (headers != null) {
                httpClient = OkHttpClientFactory.getClient(headers);
            } else {
                httpClient = OkHttpClientFactory.getClient();
            }
        }
        // add download Interceptor
        if (progressListener != null) {
            Loger.i("createApiClient","createApiClient-------0--------");
//            httpClient.newBuilder().addNetworkInterceptor(new DownloadProgressInterceptor(progressListener)).build();
            httpClient.newBuilder().addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Loger.i("createApiClient","createApiClient-------1--------");
                    Response originalResponse = chain.proceed(chain.request());
                    return originalResponse.newBuilder()
                            .body(new ProgressResponseBody(originalResponse.body(), progressListener))
                            .build();
                }
            }).build();
        }
        Retrofit retrofit = builder.client(httpClient).build();
        return retrofit.create(serviceClass);
    }

    public static boolean isEmptry(CharSequence str) {
        return str == null || str.length() == 0 || "null".equalsIgnoreCase(str.toString());
    }
}

